package com.somnus.androidkotlindemo.adapter

import android.util.Log
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.entity.MultiItemEntity
import com.somnus.androidkotlindemo.R
import com.somnus.androidkotlindemo.bean.Level0Item
import com.somnus.androidkotlindemo.bean.Level1Item

/**
 * Created by Somnus on 2019/5/27.
 */
class ExpandableListAdapter : BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    companion object {
        val TYPE_LEVEL_0 = 0
        val TYPE_LEVEL_1 = 1
    }

    constructor(list: List<MultiItemEntity>) : super(list) {
        addItemType(TYPE_LEVEL_0, R.layout.item_expandable_lv0)
        addItemType(TYPE_LEVEL_1, R.layout.item_expandable_lv1)
    }

    override fun convert(helper: BaseViewHolder, item: MultiItemEntity?) {

        when (helper.itemViewType) {
            TYPE_LEVEL_0 -> {
                val lv0: Level0Item = item as Level0Item

                helper.setText(R.id.title_parent, lv0.title)
                helper.setImageResource(R.id.iv_parent, if (lv0.isExpanded) R.mipmap.arrow_b else R.mipmap.arrow_r)
                helper.itemView.setOnClickListener {
                    val pos = helper.adapterPosition
                    Log.d("tag", "Level 0 item pos: " + pos)
                    if (lv0.isExpanded) {
                        collapse(pos)
                    } else {
                        expand(pos)
                    }
                }
            }
            TYPE_LEVEL_1 -> {
                val lv1 = item as Level1Item
                helper.setText(R.id.title, lv1.title + " parent pos: " + getParentPosition(lv1))
//                helper.setText(R.id.title, lv1.title)
//                        .setText(R.id.sub_title, lv1.subTitle)
//                        .setImageResource(R.id.iv, if (lv1.isExpanded()) R.mipmap.arrow_b else R.mipmap.arrow_r)
            }

        }
    }

}

